package by.scsbsu.database.dao;

import by.scsbsu.database.bean.StudyCourseGroup;
import by.scsbsu.database.tools.DbConnectionPool;

public class StudyCourseGroupDAO extends AbstractDAO {
	public static final String SQL_STUDY_COURSE_GROUP_CREATE_STUDY_COURSE_GROUP = "INSERT INTO study_course_group ( id_user , id_study_course ) VALUES ( ? , ? );";
	public static final String SQL_STUDY_COURSE_GROUP_GET_ID_CREATE_STUDY_COURSE_GROUP = "SELECT id FROM study_course_group WHERE ( ( id_user = ? ) AND ( id_study_course = ? ) );";
	public static final String SQL_STUDY_COURSE_GROUP_DELETE_STUDY_COURSE_GROUP_BY_ID = "DELETE FROM study_course_group WHERE ( id = ? );";
	
	
	public StudyCourseGroup createStudyCourseGroup(StudyCourseGroup studyCourseGroup){
		try {
			connection = DbConnectionPool.getConnection();
			
			statement = connection.prepareStatement(SQL_STUDY_COURSE_GROUP_CREATE_STUDY_COURSE_GROUP);
			statement.setInt(1, studyCourseGroup.getIdUser());
			statement.setInt(2, studyCourseGroup.getIdStudyCourse());
			statement.executeUpdate();
			
			statement = connection.prepareStatement(SQL_STUDY_COURSE_GROUP_GET_ID_CREATE_STUDY_COURSE_GROUP);
			statement.setInt(1, studyCourseGroup.getIdUser());
			statement.setInt(2, studyCourseGroup.getIdStudyCourse());
			
			resultSet = statement.executeQuery();
			if(resultSet.next()){
				studyCourseGroup.setId(resultSet.getInt("id"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return studyCourseGroup;
	}
	
	public void deleteStudyCourseGroupById(int id){
		try {
			connection = DbConnectionPool.getConnection();
			
			statement = connection.prepareStatement(SQL_STUDY_COURSE_GROUP_DELETE_STUDY_COURSE_GROUP_BY_ID);
			statement.setInt(1, id);
			statement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}
}
